﻿$(function () {
	$("#tabs").tabs({
		activate : function (event, ui) {
			var oldId = ui.oldTab.attr("id");
			var newId = ui.newTab.attr("id");
			var oldImg = "images/" + oldId + "-img.png";
			var newImg = "images/" + newId + "-img-active.png";
			if (oldId) {
				$("img", ui.oldTab).attr("src", oldImg);
			}
			$("img", ui.newTab).attr("src", newImg);
		}
	});

	var dialog = $("#dialog-form").dialog({
			autoOpen : false,
			height : 350,
			width : 500,
			modal : true,
			buttons : {
				"שלח" : function () {
					sendRequest();
				},
				"בטל" : function () {
					dialog.dialog("close");
				}
			},
			close : function () {
				form[0].reset();
			}
		});

	var form = dialog.find("form").on("submit", function (event) {
			event.preventDefault();
			sendRequest();
			dialog.dialog("close");
		});

	var subject = "";
	var name = form.find("#name");
	var email = form.find("#email");
	var phone = form.find("#phone");
	var allFields = $([]).add(name).add(email).add(phone);
	var tips = $(".validateTips");

	function updateTips(t) {
		tips
		.text(t)
		.addClass("ui-state-highlight");
		setTimeout(function () {
			tips.removeClass("ui-state-highlight", 1500);
		}, 500);
	}

	function checkLength(o, n, min, max) {
		if (o.val().length > max || o.val().length < min) {
			o.addClass("ui-state-error");
			updateTips("אורך השדה '" + n + "' חייב להיות בין " +
				min + " ו- " + max + ".");
			return false;
		} else {
			return true;
		}
	}

	function sendRequest() {
		allFields.removeClass("ui-state-error");
		var valid = true;
		valid = valid && checkLength(name, "שם", 2, 80);
		valid = valid && checkLength(email, 'דוא"ל', 6, 80);
		valid = valid && checkLength(phone, "טלפון", 5, 32);

		if (valid) {
			var formData = new FormData();
			formData.append("name", name.val());
			formData.append("email", email.val());
			formData.append("phone", phone.val());
			formData.append("subject", subject);
			var order = $("#tabs .ui-tabs-panel[aria-hidden='false'] .quiteResult").html();
			formData.append("order", order);
			updateTips("המתן לשליחת הבקשה!!!");
			$.ajax({
				url : 'send.php',
				data : formData,
				processData : false,
				contentType : false,
				type : 'POST',
				success : function (data) {
					$("<div title='הודעת מערכת'>בקשתך נשלחה בהצלחה</div>").dialog({
						close : function (event, ui) {
							dialog.dialog("close");
						},
						modal: true
					});
				},
				error : function (jqXHR, textStatus, errorThrown) {
					$("<div title='שליחת בקשתך נכשלה'>" + textStatus +
						": " + errorThrown + "</div>").dialog({
						close : function (event, ui) {
							dialog.dialog("close");
						},
						modal: true
					});
				}
			});
		}
	}

	$(".btn-get-price").on("click", function () {
		subject = "מחשבון המסגריה - לקוח ביקש הצעת מחיר";
		dialog.dialog("open");
	});

	$(".btn-send-order").on("click", function () {
		subject = "מחשבון המסגריה - לקוח ביצע הזמנה";
		dialog.dialog("open");
	});
});

var a = 5;
var b = 2;
var h = 3;
var d = 1 / 15;
function shorten(num) {
	return Math.ceil(num * 100) / 100;
};

function calcMaterials() {
	var x = document.getElementById("x");
	var y = document.getElementById("y");
	var o = document.getElementById("o");
	var r = document.getElementById("result");
	var size = ((x.value / 100) * y.value * o.value);
	var cement = shorten(size * a);
	var sand = shorten(size * b);
	var gravel = shorten(size * h);
	var net = shorten((size * d) * 10);
	var html = "<div class='inner'><div class='innerHead'>אתה זקוק לחומרים הבאים:</div>";
	html = html + "<label class='resultLabel'>סך הכל:</label>" + size + " קוב<br/>";
	html = html + "<label class='resultLabel'>מלט:</label>" + cement + " שקים<br/>";
	html = html + "<label class='resultLabel'>חול:</label>" + sand + " בלות<br/>";
	html = html + "<label class='resultLabel'>חצץ:</label>" + gravel + " בלות<br/>";
	html = html + "<label class='resultLabel'>רשת:</label>" + net + " <br/>";
	html = html + "רשת (15 מטר רבוע)<br/>";
	html = html + " <h4>מומלץ להוסיף 10% להזמנה</h4></div>";
	r.innerHTML = html;
	$(r).addClass("boldResult");
	$("#panel-0 .reserveButton").show();
};

var woods = {
	thinNotfeder : 1,
	wideNotfeder : 2,
	log : 5,
	gallery : 6,
	deck : 8,
	meroaf : 7
};

function calcMetrage() {
	var metrage = document.getElementById("metrage");
	var woodKind = document.getElementById("woodKind");
	var metrageKind = document.getElementById("metrageKind");
	var woodResult = document.getElementById("woodResult");
	var value = shorten(metrageKind.value == 1 ? metrage.value / woods[woodKind.value] : metrage.value * woods[woodKind.value]);
	var units = metrageKind.value == 1 ? "מטר רבוע" : "מטר רץ";
	var html = "<div class='inner'><div class='innerHead'>ערך ההמרה הוא:</div>" + value + " " + units + "<br/>";
	html = html + "<h4>מומלץ להוסיף 10% להזמנה</h4></div>";
	woodResult.innerHTML = html;
	$(woodResult).addClass("boldResult");
	$("#panel-1 .reserveButton").show();
};

function calcTiah() {
	var tiahArea = document.getElementById("tiahArea");
	var r = document.getElementById("tiahResult");
	var tit = tiahArea.value * (10 / 15) / 10;
	var melet = tiahArea.value * (10 / 05) / 10;
	var bigibond = tiahArea.value * (10 / 6) / 10;
	var html = "<div class='inner'><div class='innerHead'>אתה זקוק לחומרים דלקמן:</div><label class='resultLabel'>טיט:</label>" + shorten(tit) + " בלות<br/>";
	html = html + "<label class='resultLabel'>מלט:</label>" + shorten(melet) + " שקים (50 ק\"ג)<br/>";
	html = html + "<label class='resultLabel'>ביג'יבונד:</label>" + shorten(bigibond) + " ליטר<br/>";
	html = html + "<h4>מומלץ להוסיף 10% להזמנה</h4></div>";
	r.innerHTML = html;
	$(r).addClass("boldResult");
	$("#panel-2 .reserveButton").show();
};

function calcTit() {
	var titArea = document.getElementById("titArea");
	var r = document.getElementById("titResult");
	var titT = titArea.value * (10 / 15) / 10;
	var meletT = titArea.value * (10 / 05) / 10;
	var bigibondT = titArea.value * (10 / 6) / 10;
	var html = "<div class='inner'><div class='innerHead'>אתה זקוק לחומרים דלקמן:</div><label class='resultLabel'>טיט:</label>" + shorten(titT) + " בלות<br/>";
	html = html + "<label class='resultLabel'>מלט:</label>" + shorten(meletT) + " שקים (50 ק\"ג)<br/>";
	html = html + "<label class='resultLabel'>ביג'יבונד:</label>" + shorten(bigibondT) + " ליטר<br/>";
	html = html + "<h4>מומלץ להוסיף 10% להזמנה</h4></div>";
	r.innerHTML = html;
	$(r).addClass("boldResult");
	$("#panel-3 .reserveButton").show();
};

var co = {
	soper2000 : 100 / 8,
	soper : 100 / 6.5,
	wonder : 100 / 4.2,
	poli : 100 / 6,
	gmar : 100 / 7
};

function calcColor() {
	var meterArea = document.getElementById("meterArea");
	var color = document.getElementById("color");
	var r = document.getElementById("colorResult");
	var colorA = (meterArea.value * co[color.value]) / 100;
	var html = "<div class='inner'><div class='innerHead'>כמות הצבע לה אתה זקוק היא:</div>";
	html = html + shorten(colorA);
	html = html + ' ' + 'ליטר' + "<br/>";
	html = html + "<h4>מומלץ להוסיף 10% להזמנה</h4></div>";
	r.innerHTML = html;
	$(r).addClass("boldResult");
	$("#panel-4 .reserveButton").show();
};
var IC = {
	oncr : 100 / 9,
	zvia : 100 / 8,
	polior : 100 / 10,
	yesod : 100 / 6.5,
	dtm : 100 / 5,
	hmer : 100 / 9
};

function calcIron() {
	var ironArea1 = document.getElementById("ironArea1");
	var ironArea2 = document.getElementById("ironArea2");
	var ironMeArea = document.getElementById("ironMeArea");
	var colorI = document.getElementById("colorI");
	var propil = ((ironArea1.value * 2 + ironArea2.value * 2) / 100000);
	var proCol = propil * IC[colorI.value];
	var meter = proCol * ironMeArea.value;
	var r = document.getElementById("IronResult");
	var html = "<div class='inner'><div class='innerHead'>כמות הצבע לה אתה זקוק היא:</div>";
	html = html + shorten(meter);
	html = html + ' ' + 'ליטר' + "<br/>";
	html = html + "<h4>מומלץ להוסיף 10% להזמנה</h4></div>";
	r.innerHTML = html;
	$(r).addClass("boldResult");
	$("#panel-5 .reserveButton").show();
};

var WC = {
	varnit : 100 / 15,
	bondex : 100 / 13,
	yacht : 100 / 12,
	poliorW : 100 / 10
};

function woodColor() {
	var woodArea1 = document.getElementById("woodArea1");
	var woodArea2 = document.getElementById("woodArea2");
	var woodMArea = document.getElementById("woodMArea");
	var colorW = document.getElementById("colorW");
	var cora = ((woodArea1.value * 2 + woodArea2.value * 2) / 1000);
	var coraColor = cora * WC[colorW.value];
	var meterW = coraColor * woodMArea.value;
	var r = document.getElementById("WoodResult");
	var html = "<div class='inner'><div class='innerHead'>כמות הצבע לה אתה זקוק היא:</div>";
	html = html + shorten(meterW);
	html = html + ' ' + 'ליטר' + "<br/>";
	html = html + "<h4>מומלץ להוסיף 10% להזמנה</h4></div>";
	r.innerHTML = html;
	$(r).addClass("boldResult");
	$("#panel-6 .reserveButton").show();
};

function shorten1(num) {
	return Math.round(num)
};

function calcGeves() {
	var gMetr = document.getElementById("gMetr");
	var kind = document.getElementById("kind");
	var gHeight = document.getElementById("gHeight");
	var gNizav = document.getElementById("gNizav");
	var boreg = document.getElementById("boreg");
	var n = parseInt(gNizav.value);

	var r = document.getElementById("gevesResult");
	var plata;
	if (kind.value == "chad")
		plata = shorten1(gMetr.value * 100 / 120);
	else
		plata = shorten1((gMetr.value * 100 / 120) * 2);

	var nizav;
	if (n == 40)
		nizav =

			((gMetr.value * 100 / 120) * 3) + 2;
	else
		nizav =

			((gMetr.value * 100 / 120) * 2) + 2;

	var BrL = ((shorten1(gHeight.value / boreg.value)) + 1) * ((120 / n) + 1);
	var maslol = shorten1((gMetr.value * 100 / 120 * 2) / 3);
	var bragim = plata * BrL;
	var seret = (gHeight.value / 100) * plata + 1;
	var bidod = (gMetr.value * 100 / 120) * gHeight.value / 100;
	var html = "<div class='inner'><div class='innerHead'>אתה זקוק לחומרים דלקמן:</div>";
	html = html + plata + " " + "פלטות גבס <br/>";
	html = html + shorten1(nizav) + " " + "ניצבים <br/>";
	html = html + maslol + " " + "מסלולים <br/>";
	html = html + bragim + " " + "ברגים <br/>";
	html = html + seret + " " + "מטר סרט <br/>";
	html = html + shorten1(bidod) + " " + "מטר רץ בידוד (רוחב 120) <br/>";
	html = html + "<h4>מומלץ להוסיף 10% להזמנה</h4></div>";
	r.innerHTML = html;
	$(r).addClass("boldResult");
	$("#panel-7 .reserveButton").show();
};
